package com.learn.mvc.utils;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

import com.learn.mvc.action.Action;
import com.learn.mvc.pojo.BaseForm;
import com.learn.mvc.pojo.ConfigBean;

public class ReflectUtils {
	public static BaseForm fullFormInstance(ConfigBean bean, HttpServletRequest request) throws Exception {
		String formClassName = bean.getFormClass();
		Class<?> clazz = Class.forName(formClassName);
		BaseForm form = (BaseForm) clazz.newInstance();
		Field[] fds = clazz.getDeclaredFields();
		for (Field field : fds) {
			field.setAccessible(true);
			field.set(form, request.getParameter(field.getName()));
			field.setAccessible(false);
		}
		return form;
	}

	public static Action getActionInstance(String className) throws Exception {
		Class<?> c = Class.forName(className);
		Action baseAction = (Action) c.newInstance();
		return baseAction;
	}
}
